package com.norma.macaron.service.oauth.controller;

import cn.hutool.core.util.StrUtil;
import com.norma.macaron.common.model.enums.CommonCode;
import com.norma.macaron.common.model.enums.CredentialType;
import com.norma.macaron.common.model.enums.ResultCode;
import com.norma.macaron.common.model.response.BaseResponse;
import com.norma.macaron.common.model.response.ResponseT;
import com.norma.macaron.service.common.client.auth.AuthServiceClient;
import com.norma.macaron.service.common.domain.auth.AuthToken;
import com.norma.macaron.service.common.domain.auth.request.LoginRequest;
import com.norma.macaron.service.oauth.service.AuthService;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * Created by zhangchaofeng on 2020/6/8.
 */
@RestController
public class AuthController implements AuthServiceClient {

    @Value("${auth.clientId}")
    String clientId;
    @Value("${auth.clientSecret}")
    String clientSecret;

    @Autowired
    AuthService authService;

    @Override
    @PostMapping(value = "/v1.0/auth/login")
    public ResponseT login(@RequestParam("userName") String userName, @RequestParam("password") String password){
        AuthToken token = authService.login(userName + "|" + CredentialType.USERNAME.name(), password, clientId, clientSecret);
        return new  ResponseT(token.getAccess_token());
    }

    @Override
    @PostMapping(value = "/v1.0/auth/sms/login")
    public ResponseT smsLogin(@RequestParam("phone") String phone, @RequestParam("smsCode") String smsCode) {

        return null;
    }

    @Override
    @PostMapping(value = "/v1.0/auth/wechat/login")
    public ResponseT wechatLogin(@RequestParam("openId") String openId, @RequestParam("tempId") String tempId) {
        return null;
    }

    @Override
    @PostMapping("/v1.0/auth/logout")
    public BaseResponse logout(@RequestParam("token") String token) {
        return null;
    }

    @Override
    @PostMapping("/v1.0/auth/jwt")
    public ResponseT userjwt(@RequestParam("uid") String uid) {
        //取出cookie中的用户身份令牌
        if(uid == null){
            return new ResponseT(CommonCode.FAIL);
        }
        //拿身份令牌从redis中查询jwt令牌
        AuthToken userToken = authService.getUserToken(uid);
        if(userToken!=null){
            //将jwt令牌返回给用户
            String jwt_token = userToken.getJwt_token();
            return new ResponseT(CommonCode.SUCCESS,jwt_token);
        }
        return null;
    }

    @Override
    @GetMapping("/v1.0/auth/getCurrentUser")
    public ResponseT getCurrentUser(String headerStr) {
        //String header = request.getHeader("Authorization");
        String token = StrUtil.subAfter(headerStr, "bearer ", false);
        return new ResponseT(Jwts.parser()
                .setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody());
    }



}
